<template>
  <!-- 面试技巧 -->
  <div class="container">
    <!-- 搜索 新增 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      label-width="80px"
      size="mini"
    >
      <div class="search">
        <el-form-item label="关键字">
          <el-input
            v-model="formInline.user"
            placeholder="根据文章标题搜索"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="formInline.region"
            filterable
            placeholder="请选择"
          >
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="searchFor">
          <el-button size="small" @click="submit">清除</el-button>
          <el-button
            size="small"
            type="primary"
            @click="onSubmit(formInline.user)"
            >搜索</el-button
          >
        </el-form-item>
      </div>

      <span class="increase">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="success"
          @click="openDrawer('add')"
        >
          新增用户
        </el-button>
      </span>
    </el-form>
    <!-- 搜索 新增 -->
    <!-- 记录 -->
    <div class="record">
      <i class="el-icon-info"></i>
      <span>共 {{ total }} 条记录</span>
    </div>
    <!-- 记录 -->
    <!-- table 表格数据区域 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column align="center" label="序号" prop="id" width="80">
      </el-table-column>
      <el-table-column label="文章标题" prop="title" width="400" />
      <el-table-column label="阅读数" prop="visits" min-width="80" />
      <el-table-column label="录入人" prop="username" min-width="80" />
      <el-table-column label="录入时间" prop="createTime" min-width="80">
      </el-table-column>
      <el-table-column label="状态" prop="state" width="100">
        <template #default="{ row }">
          <span>{{ row.state ? '已启用' : '已禁用' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180px">
        <template #default="{ row }">
          <el-button size="mini" type="text" @click="openPreview(row.id)">
            预览
          </el-button>
          <el-button size="mini" type="text" @click="openDisabled(row)">
            {{ row.state ? '禁用' : '启用' }}
          </el-button>
          <el-button
            size="mini"
            type="text"
            :disabled="row.state ? true : false"
            @click="openDrawer('edit', row.id)"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            :disabled="row.state ? true : false"
            @click="del(row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- table 表格数据区域 -->
    <!-- 分页组件区域 -->
    <div class="block">
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <el-pagination
          :current-page="queryObj.page"
          :page-size="queryObj.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :page-sizes="[10, 20, 30, 50]"
          background
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 分页组件区域 -->
    <!-- 弹窗组件 -->
    <ArticlesAdd
      ref="articlesAdd"
      :dialogFormVisible.sync="visible"
      :id="currentId"
    ></ArticlesAdd>
    <!-- 弹窗组件 -->
    <ArticlesPreview
      ref="articlesPreview"
      :dialogFormVisible.sync="isVisible"
      :id="previewId"
    ></ArticlesPreview>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles.js'
// import { openWindow } from "@/utils/openWindow.js"
import ArticlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'

export default {
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      // 搜索
      formInline: {
        user: '',
        region: ''
      },
      queryObj: {
        page: 1, // 控制当前页
        pagesize: 10 // 控制每一页有几条数据
      },
      visible: false,
      isVisible: false,
      drawerType: 'add', // 打开添加
      currentId: 0,
      previewId: 0,
      loading: false,
      list: [],
      total: null // 总记录数
    }
  },

  created () {
    this.initTable()
  },

  methods: {
    // 点击操作选项打开dialod弹窗
    async openDrawer (type, id) {
      this.drawerType = type

      if (type === 'add') {
        this.visible = true // 显示弹层
        this.currentId = id
      } else if (type === 'edit') {
        this.visible = true // 显示弹层
        this.currentId = id

        // 因为vue更新是异步，在props的currentdId修改之前，已经创建了子组件发送了请求
        this.$nextTick(() => {
          // 调用子组件上的方法
          this.$refs.articlesAdd.getDepartmentInfo()
        })
      }
    },
    // 打开预览
    async openPreview (id) {
      this.isVisible = true // 显示弹层
      this.currentId = id
    },
    // 获取table数据
    async initTable () {
      this.loading = true

      const { data } = await list(this.queryObj)
      console.log(data)

      this.list = [...data.items]
      this.total = data.counts
      // this.queryObj.page = data.page
      // this.queryObj.pageSize = data.pagesize

      this.loading = false
      // console.log(this.list)
    },
    // 更改状态
    async openDisabled (row) {
      console.log(row)
      const res = { ...row }
      if (res.state) {
        res.state = 0
      } else {
        res.state = 1
      }
      const { id, state } = res

      await changeState({ id, state })

      this.initTable()
    },
    // 每页数量变化时触发
    handleSizeChange (pagesize) {
      // console.log(`每页 ${pagesize} 条`)

      this.queryObj.pagesize = pagesize

      this.initTable()
    },
    // 当前页变化时触发
    handleCurrentChange (page) {
      // console.log(`当前页: ${page}`)

      this.queryObj.page = page

      this.initTable()
    },
    // 点击删除图标删除对应的数据
    async del (id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error',
        center: true
      })
        .then(async () => {
          console.log(id)
          await remove(id)
          this.$message({
            type: 'danger',
            message: '删除成功!'
          })
          this.initTable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 清空搜索
    submit () {
      this.formInline.user = ''
      this.formInline.region = ''
      this.initTable()
    },
    // 搜索
    onSubmit (user, region) {
      // console.log(this.list.title)
      // console.log(user)
      this.list = this.list.filter(item => {
        console.log(item.title)
        return item.title === user || item.state === region
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  margin: 10px;
  .el-form {
    display: flex;
    justify-content: space-between;
    .search {
      display: flex;
      justify-content: space-between;
      .el-form-item {
        display: flex;
        justify-content: space-between;
        margin-right: 10px;
        ::v-deep .el-input__inner {
          width: 200px;
          height: 32px;
          font-size: 13px;
        }
      }
    }
  }
  .record {
    width: 100%;
    margin-bottom: 20px;
    background-color: #f4f4f5;
    color: #909399;
    padding: 8px 16px;
    span {
      padding: 0 8px;
      font-size: 13px;
      line-height: 18px;
    }
  }
  .el-table {
    width: 100%;
    ::v-deep .el-table__header-wrapper {
      border-bottom: 2px solid #e8e8e8;
      .hidden-columns {
        background-color: #fafafa;
      }
    }
  }
}
</style>
